package edu.whu.xterm;

import edu.whu.cache.CacheAware;
import edu.whu.cache.memory.impl.WeakMemoryCache;
import edu.whu.xterm.activity.MainTabActivity;
import edu.whu.xterm.http.AuthenticateHttpClient;
import edu.whu.xterm.http.RequestBuilder;
import edu.whu.xterm.util.SharedPrefManager;
import greendroid.app.GDApplication;

import org.apache.http.client.HttpClient;

import android.util.Log;

/**
 * 
 * @author sayde
 * 
 */
public class XApplication extends GDApplication {

	private AuthenticateHttpClient client;

	private SharedPrefManager sManager;

	private volatile boolean logined;

	private volatile User mUser;
	
	private CacheAware<String, Object> cache;

	@Override
	public void onCreate() {
		super.onCreate();
		RequestBuilder.init(getApplicationContext());
		cache=new WeakMemoryCache<String, Object>();
	}

	public HttpClient getHClient() {
		if (client == null) {
			client = AuthenticateHttpClient.newInstance(null, getApplicationContext());
			client.enableCurlLogging("Xterm", Log.VERBOSE);
			// HttpParams params = client.getParams();
			// HttpConnectionParams.setStaleCheckingEnabled(params, true);
		}
		return client;
	}

	public SharedPrefManager getSharedPrefManager() {
		if (sManager == null) {
			sManager = new SharedPrefManager(getApplicationContext());
		}
		return sManager;
	}
	
	public CacheAware<String, Object> getCache(){
		return cache;
	}

	@Override
	public Class<?> getHomeActivityClass() {
		return MainTabActivity.class;
	}

	public boolean isLogined() {
		return logined;
	}

	public void setLogined(boolean logined) {
		this.logined = logined;
	}

	public void setUser(User user) {
		mUser = user;
	}

	public User getUser() {
		return mUser;
	}

}
